<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
      @submit.native.prevent
    >
      <el-form-item label="商品名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入商品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wx:wxGoodsGoods:add']"
        >新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wx:wxGoodsGoods:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wx:wxGoodsGoods:remove']"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wx:wxGoodsGoods:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="wxGoodsGoodsList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="ID" align="center" prop="id" /> -->
      <el-table-column label="商家" align="center" prop="shopId" />
      <el-table-column label="商品名称" align="center" prop="name" />
      <el-table-column label="商品主图" align="center" prop="imgurl">
        <template slot-scope="scope">
          <el-image
            :src="scope.row.imgurl"
            style="width: 80px"
            :preview-src-list="[scope.row.imgurl]"
          /> </template
        >
      </el-table-column>
      <!-- <el-table-column label="启用规格" align="center" prop="hasSpec" /> -->
      <el-table-column
        label="商品类别"
        align="center"
        prop="category"
        :formatter="categoryFormat"
      />
      <el-table-column
        label="上下架"
        align="center"
        prop="disabled"
        :formatter="disabledFormat"
      />
      <el-table-column label="评论量" align="center" prop="numComment" />
      <el-table-column label="销售量" align="center" prop="numSale" />
      <el-table-column
        label="首页推荐 "
        align="center"
        prop="recomSy"
        :formatter="recomSyFormat"
      />
      <!-- <el-table-column label="详情推荐" align="center" prop="recomXq" :formatter="recomXqFormat" />
      <el-table-column label="购物车推荐" align="center" prop="recomGwc" :formatter="recomGwcFormat" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            style="display: block; margin-left: 40%"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wx:wxGoodsGoods:edit']"
            >上架</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleRecommend(scope.row)"
            v-hasPermi="['wx:wxGoodsGoods:edit']"
            >推荐</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wx:wxGoodsGoods:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商家id" prop="shopId">
          <el-input v-model="form.shopId" placeholder="请输入商家id" />
        </el-form-item>
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入商品标题" />
        </el-form-item>
        <el-form-item label="商品分类" prop="classId">
          <el-input v-model="form.classId" placeholder="请输入商品分类" />
        </el-form-item>
        <el-form-item label="商品类型" prop="typeId">
          <el-input v-model="form.typeId" placeholder="请输入商品类型" />
        </el-form-item>
        <el-form-item label="商品品牌" prop="brandId">
          <el-input v-model="form.brandId" placeholder="请输入商品品牌" />
        </el-form-item>
        <el-form-item label="商品主图" prop="imgurl">
          <el-input v-model="form.imgurl" placeholder="请输入商品主图" />
        </el-form-item>
        <el-form-item label="商品详情" prop="note">
          <el-input v-model="form.note" placeholder="请输入商品详情" />
        </el-form-item>
        <el-form-item label="属性详情" prop="prop">
          <el-input v-model="form.prop" placeholder="请输入属性详情" />
        </el-form-item>
        <el-form-item label="规格详情" prop="spec">
          <el-input v-model="form.spec" placeholder="请输入规格详情" />
        </el-form-item>
        <el-form-item label="参数详情" prop="param">
          <el-input v-model="form.param" placeholder="请输入参数详情" />
        </el-form-item>
        <el-form-item label="启用规格" prop="hasSpec">
          <el-input v-model="form.hasSpec" placeholder="请输入启用规格" />
        </el-form-item>
        <el-form-item label="计量单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入计量单位" />
        </el-form-item>
        <el-form-item label="商品类别(0默认普通1服务)" prop="category">
          <el-input
            v-model="form.category"
            placeholder="请输入商品类别(0默认普通1服务)"
          />
        </el-form-item>
        <el-form-item label="是否下架(0上架,1下架)">
          <el-radio-group v-model="form.disabled">
            <el-radio
              v-for="dict in disabledOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
              >{{ dict.dictLabel }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="上架时间" prop="upAt">
          <el-date-picker
            clearable
            size="small"
            v-model="form.upAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择上架时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="下架时间" prop="downAt">
          <el-date-picker
            clearable
            size="small"
            v-model="form.downAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择下架时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否必须留言(0必须1不是必须)" prop="message">
          <el-input
            v-model="form.message"
            placeholder="请输入是否必须留言(0必须1不是必须)"
          />
        </el-form-item>
        <el-form-item label="浏览量" prop="numView">
          <el-input v-model="form.numView" placeholder="请输入浏览量" />
        </el-form-item>
        <el-form-item label="评论量" prop="numComment">
          <el-input v-model="form.numComment" placeholder="请输入评论量" />
        </el-form-item>
        <el-form-item label="销售量" prop="numSale">
          <el-input v-model="form.numSale" placeholder="请输入销售量" />
        </el-form-item>
        <el-form-item label="周销售量" prop="numSaleWeek">
          <el-input v-model="form.numSaleWeek" placeholder="请输入周销售量" />
        </el-form-item>
        <el-form-item label="售后服务" prop="afterSale">
          <el-input v-model="form.afterSale" placeholder="请输入售后服务" />
        </el-form-item>
        <el-form-item label="排序字段" prop="location">
          <el-input v-model="form.location" placeholder="请输入排序字段" />
        </el-form-item>
        <el-form-item label="创建用户id" prop="userIdCreate">
          <el-input
            v-model="form.userIdCreate"
            placeholder="请输入创建用户id"
          />
        </el-form-item>
        <el-form-item label="首页推荐 0：不推荐，1：推荐">
          <el-radio-group v-model="form.recomSy">
            <el-radio
              v-for="dict in recomSyOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
              >{{ dict.dictLabel }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <!-- <el-form-item label="详情推荐 0：不推荐，1：推荐">
          <el-radio-group v-model="form.recomXq">
            <el-radio
              v-for="dict in recomXqOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="购物车推荐 0：不推荐，1：推荐">
          <el-radio-group v-model="form.recomGwc">
            <el-radio
              v-for="dict in recomGwcOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <el-form-item label="邮费" prop="costMoney">
          <el-input v-model="form.costMoney" placeholder="请输入邮费" />
        </el-form-item>
        <el-form-item label="是否开发票" prop="isInvoice">
          <el-input v-model="form.isInvoice" placeholder="请输入是否开发票" />
        </el-form-item>
        <el-form-item label="创建时间" prop="gmtCreate">
          <el-date-picker
            clearable
            size="small"
            v-model="form.gmtCreate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择创建时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="修改时间" prop="gmtModified">
          <el-date-picker
            clearable
            size="small"
            v-model="form.gmtModified"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择修改时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="删除标记0-显示,1-删除" prop="delFlag">
          <el-input
            v-model="form.delFlag"
            placeholder="请输入删除标记0-显示,1-删除"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改商品对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="recommend"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="首页推荐">
          <el-radio-group v-model="form.recomSy">
            <el-radio
              v-for="dict in recomSyOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
              >{{ dict.dictLabel }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <!-- <el-form-item label="详情推荐">
          <el-radio-group v-model="form.recomXq">
            <el-radio
              v-for="dict in recomXqOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="购物车推荐">
          <el-radio-group v-model="form.recomGwc">
            <el-radio
              v-for="dict in recomGwcOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listWxGoodsGoods,
  getWxGoodsGoods,
  delWxGoodsGoods,
  addWxGoodsGoods,
  updateWxGoodsGoods,
  exportWxGoodsGoods,
} from "@/api/wx/wxGoodsGoods";

export default {
  name: "WxGoodsGoods",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品表格数据
      wxGoodsGoodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      recommend: false,
      // 商品类别(0默认普通1服务)字典
      categoryOptions: [],
      // 是否下架(0上架,1下架)字典
      disabledOptions: [],
      // 首页推荐 0：不推荐，1：推荐字典
      recomSyOptions: [],
      // 详情推荐 0：不推荐，1：推荐字典
      recomXqOptions: [],
      // 购物车推荐 0：不推荐，1：推荐字典
      recomGwcOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        shopId: [
          { required: true, message: "商家id不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("wx_goods_category").then((response) => {
      this.categoryOptions = response.data;
    });
    this.getDicts("wx_goods_disabled").then((response) => {
      this.disabledOptions = response.data;
    });
    this.getDicts("men_recommend_status").then((response) => {
      this.recomSyOptions = response.data;
    });
    this.getDicts("men_recommend_status").then((response) => {
      this.recomXqOptions = response.data;
    });
    this.getDicts("men_recommend_status").then((response) => {
      this.recomGwcOptions = response.data;
    });
  },
  methods: {
    /** 查询商品列表 */
    getList() {
      this.loading = true;
      listWxGoodsGoods(this.queryParams).then((response) => {
        this.wxGoodsGoodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 商品类别(0默认普通1服务)字典翻译
    categoryFormat(row, column) {
      return this.selectDictLabel(this.categoryOptions, row.category);
    },
    // 是否下架(0上架,1下架)字典翻译
    disabledFormat(row, column) {
      return this.selectDictLabel(this.disabledOptions, row.disabled);
    },
    // 首页推荐 0：不推荐，1：推荐字典翻译
    recomSyFormat(row, column) {
      return this.selectDictLabel(this.recomSyOptions, row.recomSy);
    },
    // 详情推荐 0：不推荐，1：推荐字典翻译
    recomXqFormat(row, column) {
      return this.selectDictLabel(this.recomXqOptions, row.recomXq);
    },
    // 购物车推荐 0：不推荐，1：推荐字典翻译
    recomGwcFormat(row, column) {
      return this.selectDictLabel(this.recomGwcOptions, row.recomGwc);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.recommend = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        shopId: null,
        name: null,
        title: null,
        classId: null,
        typeId: null,
        brandId: null,
        imgurl: null,
        note: null,
        prop: null,
        spec: null,
        param: null,
        hasSpec: null,
        unit: null,
        category: null,
        disabled: 0,
        upAt: null,
        downAt: null,
        message: null,
        numView: null,
        numComment: null,
        numSale: null,
        numSaleWeek: null,
        afterSale: null,
        location: null,
        userIdCreate: null,
        recomSy: 0,
        recomXq: 0,
        recomGwc: 0,
        costMoney: null,
        isInvoice: null,
        gmtCreate: null,
        gmtModified: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getWxGoodsGoods(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品";
      });
    },

    /** 推荐按钮操作 */
    handleRecommend(row) {
      this.reset();
      const id = row.id || this.ids;
      getWxGoodsGoods(id).then((response) => {
        this.form = response.data;
        this.recommend = true;
        this.title = " 推荐商品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateWxGoodsGoods(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.recommend = false;
              this.getList();
            });
          } else {
            addWxGoodsGoods(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.recommend = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除商品选中数据", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delWxGoodsGoods(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有商品数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportWxGoodsGoods(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
  },
};
</script>
